﻿// ===============================================================================
// Project Name        :    Gfw.Repositories.Account
// Project Description :    
// ===============================================================================
// Class Name          :    ActUserDynamicawardRepository
// Namespace:          :    Gfw.Repositories.Account
// Class Version       :    v1.0.0.0
// Class Description   :    
// CLR Version:        :    4.0.30319.17626
//Author               :    lif
// Machine             :    ADICO-PC
// Create Time         :    2012/6/24 10:24:30
// Update Time         :    2012/6/24 10:24:30
// ===============================================================================
// Copyright © lif 2012 . All rights reserved.
// ===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.Models.Account;
using Gfw.Repository;
using NHibernate.Criterion;

namespace Gfw.Repositories.Account
{
    public class ActUserDynamicawardRepository : Repository<ActUserDynamicaward>
    {

        public virtual void AddAward(string userId, int type, decimal receipt, decimal actuality,
            decimal taxation, string awardSerialNo = null,
            UnitOfWork unitOfWork = null)
        {
            ActUserDynamicaward aud = new ActUserDynamicaward()
                                          {
                                              Id = Guid.NewGuid().ToString(),
                                              Userid = userId,
                                              AwardDate = DateTime.Now,
                                              TradeType = type,
                                              AwardReceipt = receipt,
                                              AwardActuality = actuality,
                                              Taxation = taxation,
                                              AwardSerialNo = awardSerialNo

                                          };
            Save(aud, unitOfWork);
        }

        public IList<ActUserDynamicaward> Query(string userId,int tradeType, DateTime? start, DateTime? end, int pageIndex, int pageSize, out int totalPage)
        {
            var query = Session.CreateCriteria(typeof(ActUserDynamicaward));
            query.Add(Expression.Eq("Userid", userId));
            if (tradeType >= 0)
            {
                query.Add(Expression.Eq("TradeType", tradeType));
            }

            if (start.HasValue)
            {
                query.Add(Expression.Ge("AwardDate", start));
            }
            if (end.HasValue)
            {
                end.Value.AddDays(1);
                query.Add(Expression.Le("AwardDate", end));
            }
            totalPage = (query.List().Count + pageSize - 1) / pageSize;

            return query.SetFirstResult(pageIndex * pageSize).SetMaxResults(pageSize).List<ActUserDynamicaward>();
        }
    }
}
